package com.jiyun.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiyun.om.entity.PickupOrder;
import com.jiyun.om.entity.Waybill;
import com.jiyun.result.Result;
import com.jiyun.service.IPickupOrderService;
import com.jiyun.service.IWaybillService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 提货单表 前端控制器
 * </p>
 *
 * @author zhx
 * @since 2025-02-12
 */
@RestController
@RequestMapping("/pickup-order")
@Tag(name = "提货单管理")
public class PickupOrderController {
    @Autowired
    private IPickupOrderService pickupOrderService;
    @Autowired
    private IWaybillService waybillService;

    @Operation(summary = "分页查询提货单")
    @RequestMapping("/getAll")
    public Page<PickupOrder> getAll(Integer pageNum, Integer pageSize, @RequestBody PickupOrder pickupOrder) {
        return pickupOrderService.getAll(pageNum, pageSize, pickupOrder);
    }

    @Operation(summary = "删除提货单")
    @RequestMapping("/toDelete")
    public Result toDelete(@RequestParam Integer id) {

        pickupOrderService.removeById(id);
        return Result.ok();
    }

    @Operation(summary = "提货单详情")
    @RequestMapping("/XiangQing")
    public PickupOrder XiangQing(@RequestBody PickupOrder pickupOrder) {
        return pickupOrderService.getById(pickupOrder.getId());
    }

    @Operation(summary = "修改是否到站")
    @RequestMapping("/updateIsArrived")
    public Result updateIsArrived(@RequestParam Integer id) {
        PickupOrder pickupOrder = pickupOrderService.getById(id);
        if (pickupOrder != null && pickupOrder.getIsArrived().equals("未到站")) {
            pickupOrder.setIsArrived("已到站");
            // 获取运单外键id
            Integer waybillId = pickupOrder.getWid();
            if (waybillId != null) {
                Waybill waybill = waybillService.getById(waybillId);
                if (waybill != null) {
                    waybill.setActualArrivalTime(new Date());
                    waybillService.updateById(waybill);
                }
            }
            pickupOrderService.updateById(pickupOrder);
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @Operation(summary = "动态查询提货单")
    @RequestMapping("/findPickupOrder")
    public List<PickupOrder> findPickupOrder() {
        return pickupOrderService.list();
    }
    @Operation(summary = "导出")
    @RequestMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response) throws IOException {
        List<PickupOrder> list = pickupOrderService.findAll();
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet();
        XSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("运单号");
        row.createCell(1).setCellValue("始发地");
        row.createCell(2).setCellValue("目的地");
        row.createCell(3).setCellValue("提货状态");
        row.createCell(4).setCellValue("提货单");
        row.createCell(5).setCellValue("订单号");
        row.createCell(6).setCellValue("计划发车时间");
        row.createCell(7).setCellValue("实际发车时间");
        row.createCell(8).setCellValue("计划到达时间");
        row.createCell(9).setCellValue("实际到达时间");
        row.createCell(10).setCellValue("提货开始时间");
        row.createCell(11).setCellValue("提货完成时间");
        row.createCell(12).setCellValue("创建人");
        row.createCell(13).setCellValue("创建时间");
        row.createCell(14).setCellValue("提货重量");
        row.createCell(15).setCellValue("提货体积");
        row.createCell(16).setCellValue("车牌号");
        row.createCell(17).setCellValue("手机号码");
        row.createCell(18).setCellValue("司机");
        row.createCell(19).setCellValue("提货数量");
        row.createCell(20).setCellValue("收货地址");
        row.createCell(21).setCellValue("提货运费合计");
        row.createCell(22).setCellValue("客户");
        row.createCell(23).setCellValue("发货地址");
        row.createCell(24).setCellValue("是否到站");
        for (int i = 0; i < list.size(); i++) {
            PickupOrder pickupOrder = list.get(i);
            XSSFRow xssfRow = sheet.createRow(i + 1);
            xssfRow.createCell(0).setCellValue(pickupOrder.getWaybillId());
            xssfRow.createCell(1).setCellValue(pickupOrder.getOrigin());
            xssfRow.createCell(2).setCellValue(pickupOrder.getDestination());
            xssfRow.createCell(3).setCellValue(pickupOrder.getPickupStatus());
            xssfRow.createCell(4).setCellValue(pickupOrder.getPickupOrderId());
            xssfRow.createCell(5).setCellValue(pickupOrder.getOrderId());
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = simpleDateFormat.format(pickupOrder.getPlannedDepartureTime());
            xssfRow.createCell(6).setCellValue(format);
            String format1 = simpleDateFormat.format(pickupOrder.getActualDepartureTime());
            xssfRow.createCell(8).setCellValue(format1);
            String format2 = simpleDateFormat.format(pickupOrder.getPlannedArrivalTime());
            xssfRow.createCell(7).setCellValue(format2);
            String format3 = simpleDateFormat.format(pickupOrder.getActualArrivalTime());
            xssfRow.createCell(9).setCellValue(format3);
            String format4 = simpleDateFormat.format(pickupOrder.getPickupStartTime());
            xssfRow.createCell(10).setCellValue(format4);
            String format5 = simpleDateFormat.format(pickupOrder.getPickupCompletionTime());
            xssfRow.createCell(11).setCellValue(format5);
            xssfRow.createCell(12).setCellValue(pickupOrder.getCreator());
            String format6 = simpleDateFormat.format(pickupOrder.getCreationTime());
            xssfRow.createCell(13).setCellValue(format6);
            xssfRow.createCell(14).setCellValue(pickupOrder.getTotalWeight().doubleValue());
            xssfRow.createCell(15).setCellValue(pickupOrder.getTotalVolume().doubleValue());
            xssfRow.createCell(16).setCellValue(pickupOrder.getLicensePlate());
            xssfRow.createCell(17).setCellValue(pickupOrder.getDriverPhone());
            xssfRow.createCell(18).setCellValue(pickupOrder.getDriverName());
            xssfRow.createCell(19).setCellValue(pickupOrder.getQuantity());
            xssfRow.createCell(20).setCellValue(pickupOrder.getReceivingAddress());
            xssfRow.createCell(21).setCellValue(pickupOrder.getTotalPayment().doubleValue());
            xssfRow.createCell(22).setCellValue(pickupOrder.getCustomer());
            xssfRow.createCell(23).setCellValue(pickupOrder.getShippingAddress());
            xssfRow.createCell(24).setCellValue(pickupOrder.getIsArrived());
        }
        response.setHeader("Content-Disposition", "attachment;filename=PickupOrder.xlsx");
        workbook.write(response.getOutputStream());
        workbook.close();

    }
}
